"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
// Native
var path_1 = require("path");
// Packages
var electron_1 = require("electron");
var electron_is_dev_1 = require("electron-is-dev");
var node_path_1 = require("node:path");
var store_1 = require("./store");
var AddStockWindow_1 = require("./AddStockWindow");
var tray = null;
var p = node_path_1["default"].join(__dirname, "resources", "icon.ico");
console.log(p);
var icon = electron_1.nativeImage.createFromPath(p);
function createWindow() {
    // Create the browser window.
    var window = new electron_1.BrowserWindow({
        icon: icon,
        width: 120,
        height: 120,
        type: "toolbar",
        frame: false,
        resizable: false,
        transparent: true,
        alwaysOnTop: true,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: true,
            preload: node_path_1["default"].join(__dirname, "./preload.js"),
            webSecurity: false
        }
    });
    // 通过获取用户屏幕的宽高来设置悬浮球的初始位置
    var _a = {
        left: electron_1.screen.getPrimaryDisplay().workAreaSize.width - 160,
        top: electron_1.screen.getPrimaryDisplay().workAreaSize.height - 160
    }, left = _a.left, top = _a.top;
    window.setPosition(left, top); // 设置悬浮球位置
    var port = process.env.PORT || 3000;
    var url = electron_is_dev_1["default"]
        ? "http://localhost:".concat(port)
        : (0, path_1.join)(__dirname, "../src/out/index.html");
    // and load the index.html of the app.
    if (electron_is_dev_1["default"]) {
        window === null || window === void 0 ? void 0 : window.loadURL(url);
    }
    else {
        window === null || window === void 0 ? void 0 : window.loadFile(url);
    }
    // Open the DevTools.
    // window.webContents.openDevTools();
    electron_1.ipcMain.on("suspensionWindowMove", function (_event, message) {
        window.setPosition(message.x, message.y);
    });
    // For AppBar
    electron_1.ipcMain.on("minimize", function () {
        // eslint-disable-next-line no-unused-expressions
        window.isMinimized() ? window.restore() : window.minimize();
        // or alternatively: win.isVisible() ? win.hide() : win.show()
    });
    electron_1.ipcMain.on("maximize", function () {
        // eslint-disable-next-line no-unused-expressions
        window.isMaximized() ? window.restore() : window.maximize();
    });
    electron_1.ipcMain.on("close", function () {
        window.close();
    });
    // 禁止窗口最大化
    window.setMaxListeners(1);
}
function createTray() {
    // 创建一个原生图像对象，这里使用PNG格式的图标示例
    // 创建托盘实例
    tray = new electron_1.Tray(icon);
    // 创建上下文菜单
    var contextMenu = electron_1.Menu.buildFromTemplate([
        { label: "添加龙股", click: AddStockWindow_1["default"] },
        { label: "打开应用", click: createWindow },
        { label: "退出", click: function () { return electron_1.app.quit(); } },
    ]);
    // 设置托盘的上下文菜单
    tray.setContextMenu(contextMenu);
    // 设置托盘提示信息
    tray.setToolTip("股海擒龙");
    tray.on("click", function () {
        createWindow();
    });
    // // 监听托盘销毁事件
    // tray.on("destroyed", () => {
    //   tray = null;
    // });
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
electron_1.app.whenReady().then(function () {
    createWindow();
    createTray();
    electron_1.app.on("activate", function () {
        // On macOS it's common to re-create a window in the app when the
        // dock icon is clicked and there are no other windows open.
        if (electron_1.BrowserWindow.getAllWindows().length === 0)
            createWindow();
    });
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
electron_1.app.on("window-all-closed", function () {
    if (process.platform !== "darwin")
        electron_1.app.quit();
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
// listen the channel `message` and resend the received message to the renderer process
electron_1.ipcMain.on("message", function (event, message) {
    console.log(message);
    setTimeout(function () { return event.sender.send("message", "hi from electron"); }, 500);
});
electron_1.ipcMain.on("getStocks", function (event) {
    // eslint-disable-next-line no-param-reassign,no-console
    // eslint-disable-next-line no-param-reassign
    event.returnValue = store_1["default"].get("stocks");
});
electron_1.ipcMain.on("hideMainWindow", function (_event) {
    var _a;
    (_a = electron_1.BrowserWindow.getFocusedWindow()) === null || _a === void 0 ? void 0 : _a.hide();
});
electron_1.ipcMain.on("addStock", function (event, stockCode) {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    if (!stockCode)
        return;
    var stocks = store_1["default"].get("stocks");
    if (!stocks.includes(stockCode)) {
        store_1["default"].set("stocks", __spreadArray(__spreadArray([], stocks, true), [stockCode], false));
    }
    // eslint-disable-next-line no-param-reassign
    event.returnValue = store_1["default"].get("stocks");
});
electron_1.ipcMain.on("delStock", function (event, stockCode) {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    store_1["default"].set("stocks", store_1["default"].get("stocks").filter(function (item) { return item !== stockCode; }));
    // eslint-disable-next-line no-param-reassign
    event.returnValue = store_1["default"].get("stocks");
});
electron_1.ipcMain.on("closeAddStockWindow", function (_event) {
    var _a;
    (_a = electron_1.BrowserWindow.getFocusedWindow()) === null || _a === void 0 ? void 0 : _a.hide();
});
